//------------------------------------------------------------------------------
// <copyright file="SerializableKeyGesture.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Configuration;
using System.ComponentModel;

namespace DAnton.Utilities.Helpers.KeyBindings
{
    [TypeConverter(typeof(SerializableKeyGestureConverter))]
    [SettingsSerializeAs(SettingsSerializeAs.String)]
    public class SerializableKeyGesture : InputGesture, INotifyPropertyChanged, ICloneable
    {
        #region private members

        Key key;
        ModifierKeys modifier;
        string description;
        string ownerWindowName;

        #endregion

        #region properties

        public event PropertyChangedEventHandler PropertyChanged;

        public string OwnerWindowName
        {
            get { return this.ownerWindowName; }
            set
            {
                this.ownerWindowName = value;
                this.OnPropertyChanged("OwnerWindowName");
            }
        }

        public string Description
        {
            get { return this.description; }
            set
            {
                this.description = value;
                this.OnPropertyChanged("Description");
            }
        }

        public Key Key
        {
            get { return this.key; }
            set 
            { 
                this.key = value;
                this.OnPropertyChanged("Key");
            }
        }

        public ModifierKeys Modifier
        {
            get { return this.modifier; }
            set 
            { 
                this.modifier = value;
                this.OnPropertyChanged("Modifier");
            }
        }

        #endregion

        #region constructors

        public SerializableKeyGesture(Key key, string ownerWindowName, string description)
            : this(key, ModifierKeys.None, ownerWindowName, description)
        {
        }

        public SerializableKeyGesture(Key key, ModifierKeys modifier, string ownerWindowName, string description)
        {
            this.key = key;
            this.modifier = modifier;
            this.ownerWindowName = ownerWindowName;
            this.description = description;
        }

        #endregion

        #region public methods

        public SerializableKeyGesture Clone()
        {
            return new SerializableKeyGesture(this.key, this.modifier, this.ownerWindowName, this.description);
        }

        #region ICloneable Members

        object ICloneable.Clone()
        {
            return this.Clone();
        }

        #endregion

        public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
        {
            if (inputEventArgs is KeyEventArgs)
            {
                KeyEventArgs args = inputEventArgs as KeyEventArgs;

                if (args.Key == this.key &&
                    Keyboard.Modifiers == this.modifier)
                {
                    return true;
                }

                return false;
            }
            else
                return false;
        }

        #endregion

        #region private methods

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
